package com.neu.exception;



import com.neu.common.Result;
import com.sun.mail.smtp.SMTPAddressFailedException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailSendException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice(basePackages = "com.neu.controller")
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    //private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //统一异常处理@ExceptionHandler，主要用于Exception
//    @ExceptionHandler(Exception.class)
//    public Result error(HttpServletRequest request,Exception e){
//        log.error("异常信息：",e);
//        return Result.error("System Exception");
//    }

//    @ExceptionHandler(MailSendException.class)
//    public Result emailExceptionHandler(SMTPAddressFailedException ex){
//        log.error(ex.getMessage());
//        if(ex.getMessage().contains("Invalid Addresses")){
//            return Result.error("Please enter the correct shipping address");
//        }
//        return Result.error("System Exception");
//    }
    @ExceptionHandler(MailSendException.class)
    public Result emailExceptionHandler(MailSendException ex){
        log.error(ex.getMessage());
        if(ex.getMessage().contains("Invalid Addresses")){
            return Result.error("Please enter the correct email receiving address");
        }
        return Result.error("System Exception");
    }
    @ExceptionHandler(CustomException.class)
    public Result customError(HttpServletRequest request,CustomException e){
        return Result.error(e.getMsg());
    }
}
